package com.topscore.uke.mvp.model;

import com.topscore.uke.entities.CommodityData;
import com.topscore.uke.mvp.contractors.GetCommodityPageDataContractor;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.retrofit.ApiService;
import com.topscore.uke.network.retrofit.HandleErrorSubscriber;
import com.topscore.uke.network.retrofit.NetClient;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 71468 on 2018/3/13.
 */

public class GetCommodityPageModel implements GetCommodityPageDataContractor, GetCommodityPageDataContractor.Model {
    private Listener mListener;
    private ApiService mService = NetClient.getInstance().net().create(ApiService.class);

    public GetCommodityPageModel(Listener mListener) {
        this.mListener = mListener;
    }

    @Override
    public void getCommodityData() {
        mService.getCommodityPageData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<List<CommodityData>>>(mListener) {
                    @Override
                    public void onSuccess(BaseResponse<List<CommodityData>> response) {
                        mListener.onGetDataSuccess(response);
                    }
                });
    }
}
